[Oracle SQL Tricks] Generating Dates Between Two Dates


Posted by RedPanda56 on 2021-02-02

1. Goal

用一段SQL產生指定日期區間內的所有日期

2. Code

SELECT backup_date
FROM ( SELECT trunc(to_date('20210201', 'yyyymmdd') - rownum - 1) as backup_date
       FROM dual
       CONNECT BY rownum <= 5
     )

3.Result

backup_date
2021/02/01
2021/01/31
2021/01/30
2021/01/29
2021/01/28

4. Description

  • to_date(‘20210201’, ‘yyyymmdd’):可換成任何日期
  • rownum <= 5:可調整產生幾筆日期資料

5. Reference

Generate a range of dates using SQL


#Oracle SQL







Related Posts

Azure Functions with Python

Azure Functions with Python

Git 基礎

Git 基礎

For 大家族 for 迴圈、forEach、for...in、for...of 一次搞清楚

For 大家族 for 迴圈、forEach、for...in、for...of 一次搞清楚


Comments